﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using stockfund.src;
using stockfund.popform;
using MySQLDriverCS;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
namespace stockfund.popform
{
    public partial class frmHangye : Form
    {
        public frmHangye()
        {
            InitializeComponent();
            this.conn = Tools.getConn();
        }
        private List<Hangye> hangyelist = new List<Hangye>();
          
        private MySQLConnection conn;
        private void btn_hangyephoto_Click(object sender, EventArgs e)
        {

        }

        private void frmHangyestat_Load(object sender, EventArgs e)
        {
            
            string sql = "select distinct(hangye)hangye from hangyefund";
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);
            DataSet ds = new DataSet();
            MySQLDataAdapter myadp = new MySQLDataAdapter(cmd);
            myadp.Fill(ds, "hangyelist");
            this.dgv_hangyelist.DataSource = ds.Tables["hangyelist"];
            this.dgv_hangyelist.Columns[0].Name = "hangye";
            this.dgv_hangyelist.Columns[0].HeaderText = "行业";
            this.dgv_hangyelist.Columns[0].Width = 220;
            
        }
        private void getHangyeDataInType(string action)
        {
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            int days = 40, perColumnCounter = 40;
            string[] statdates = new string[days];
            Hangyefund hangyefund;
            ArrayList hangyefundlist;
            NoSortHashTable hangyetable = new NoSortHashTable();
            DataTable hangyedt = new DataTable("hangyedt");
            DataColumn dc = hangyedt.Columns.Add("rank", Type.GetType("System.String"));
            for (int i = 0; i < days; i++)
            {

                statdates[i] = DateTime.Now.AddDays(-1 * i).ToString("yyyy-MM-dd");
                dc = hangyedt.Columns.Add(statdates[i], Type.GetType("System.String"));
                string sql = "select hangye,hasstocks,sumfund,infund,percent,atype,statdate from hangyefund where statdate='" + statdates[i] + "' and atype = '" + action + "(标准行业一级)' order by rank limit " + perColumnCounter.ToString() + "";
                cmd = new MySQLCommand(sql, conn);
                DbDataReader read = cmd.ExecuteReader();
                hangyefundlist = new ArrayList();
                while (read.Read())
                {
                    hangyefund = new Hangyefund();
                    hangyefund.Hangye = read[0].ToString();
                    hangyefund.Hasstocks = Int32.Parse(read[1].ToString());
                    hangyefund.Sumfund = double.Parse(read[2].ToString());
                    hangyefund.Infund = double.Parse(read[3].ToString());
                    hangyefund.Percent = double.Parse(read[4].ToString());
                    hangyefund.aTpye = read[5].ToString();
                    hangyefund.Statdate = DateTime.Parse(read[6].ToString());
                    hangyefundlist.Add(hangyefund);
                }
                hangyetable.Add(statdates[i], hangyefundlist);
            }
            DataRow dr;
            for (int i = 0; i < perColumnCounter; i++)
            {
                dr = hangyedt.NewRow();
                hangyedt.Rows.Add(dr);
            }
            int colId = 0;
            foreach (object hangyefundlistkey in hangyetable.Keys)
            {
                string key = (string)hangyefundlistkey;
                ArrayList _hangyefundlist = (ArrayList)hangyetable[key];
                int rowid = 0;
                foreach (Hangyefund o in _hangyefundlist)
                {
                    dr = hangyedt.Rows[rowid];
                    dr[key] = o.Hangye;
                    dr = hangyedt.Rows[rowid];
                    dr[0] = rowid + 1;
                    rowid++;

                }
                colId++;
            }


            this.dgv_hangyestatlist.DataSource = hangyedt;
            this.dgv_hangyestatlist.Columns[0].HeaderText = "Rank";
        }
        private void dgv_hangyelist_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string hangye = this.dgv_hangyelist.CurrentCell.Value.ToString();
            for (int colId = 0; colId < this.dgv_hangyestatlist.Columns.Count-1; colId++)
            {
                for (int rowId = 0; rowId < this.dgv_hangyestatlist.Rows.Count - 1; rowId++)
                {
                    DataGridViewCell cell = this.dgv_hangyestatlist[colId, rowId];
                    cell.Style.BackColor = Color.White;
                        if (cell.Value.ToString() == hangye)
                        {
                            cell.Style.BackColor = Color.Red;
                        }
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.linkLabel1.LinkColor = Color.FromArgb(0,0,255);
            this.linkLabel2.LinkColor = Color.FromArgb(0, 0, 255);
            this.linkLabel3.LinkColor = Color.FromArgb(0, 0, 255);
            this.linkLabel4.LinkColor = Color.FromArgb(0, 0, 255);
            this.linkLabel5.LinkColor = Color.FromArgb(0, 0, 255);
            this.linkLabel6.LinkColor = Color.FromArgb(0, 0, 255);
            LinkLabel lbl =  sender as LinkLabel;
            lbl.LinkColor = Color.Red;
            string action = lbl.Text;
            this.getHangyeDataInType(action);
        }
    }
}
